package com.cykj.pos.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.BizMicroInfo;
import com.cykj.pos.domain.FeiShunSettlement;
import com.cykj.pos.mapper.FeiShunSettlementMapper;
import com.cykj.pos.service.IBizMicroInfoService;
import com.cykj.pos.service.IFeiShunSettlementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 结算信息Service业务层处理
 *
 * @author weijianbo
 * @date 2024-02-04
 */
@Service
public class FeiShunSettlementServiceImpl extends ServiceImpl<FeiShunSettlementMapper, FeiShunSettlement> implements IFeiShunSettlementService {
    @Autowired
    private BizWalletServiceImpl bizWalletServiceImpl;
    @Autowired
    private FeiShunSettlementMapper feiShunSettlementMapper;
    @Autowired
    private IBizMicroInfoService bizMicroInfoService;

    @Override
    public List<FeiShunSettlement> queryList(FeiShunSettlement feiShunSettlement) {
        LambdaQueryWrapper<FeiShunSettlement> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(feiShunSettlement.getTaskId())){
            lqw.eq(FeiShunSettlement::getTaskId ,feiShunSettlement.getTaskId());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getBankName())){
            lqw.like(FeiShunSettlement::getBankName ,feiShunSettlement.getBankName());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getBankNum())){
            lqw.eq(FeiShunSettlement::getBankNum ,feiShunSettlement.getBankNum());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getSettleAccount())){
            lqw.eq(FeiShunSettlement::getSettleAccount ,feiShunSettlement.getSettleAccount());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getUserName())){
            lqw.like(FeiShunSettlement::getUserName ,feiShunSettlement.getUserName());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getUserNum())){
            lqw.eq(FeiShunSettlement::getUserNum ,feiShunSettlement.getUserNum());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getUserTel())){
            lqw.eq(FeiShunSettlement::getUserTel ,feiShunSettlement.getUserTel());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getBankSubName())){
            lqw.like(FeiShunSettlement::getBankSubName ,feiShunSettlement.getBankSubName());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getCreteDate())){
            lqw.eq(FeiShunSettlement::getCreteDate ,feiShunSettlement.getCreteDate());
        }

        lqw.eq(FeiShunSettlement::getStatus ,"10");

        if (StringUtils.isNotBlank(feiShunSettlement.getErrorMessage())){
            lqw.eq(FeiShunSettlement::getErrorMessage ,feiShunSettlement.getErrorMessage());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getCpayCount())){
            lqw.eq(FeiShunSettlement::getCpayCount ,feiShunSettlement.getCpayCount());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getSettlementId())){
            lqw.eq(FeiShunSettlement::getSettlementId ,feiShunSettlement.getSettlementId());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getTaskUserId())){
            lqw.eq(FeiShunSettlement::getTaskUserId ,feiShunSettlement.getTaskUserId());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getOrderNo())){
            lqw.eq(FeiShunSettlement::getOrderNo ,feiShunSettlement.getOrderNo());
        }
        if (StringUtils.isNotBlank(feiShunSettlement.getUrl())){
            lqw.eq(FeiShunSettlement::getUrl ,feiShunSettlement.getUrl());
        }
        lqw.orderByDesc(FeiShunSettlement::getId);
        return this.list(lqw);
    }
    public AjaxResult submitSett(FeiShunSettlement feiShunSettlement){
        AjaxResult aja = AjaxResult.success("预支付提现");
        feiShunSettlement=feiShunSettlementMapper.selectById(feiShunSettlement.getId());
        BizMicroInfo bizMicroInfo = feiShunSettlementMapper.getBizMicroInfoByOrderNo(feiShunSettlement.getOrderNo());
//        if(bizMicroInfo.getMerchId() != 3l){
//            return AjaxResult.error("提现系统升级中,请关注后续通知");
//        }
        Map<String, Object> hqyResultMap = bizWalletServiceImpl.hyqWithdrawal(feiShunSettlement.getOrderNo(),feiShunSettlement.getSettleAccount(),bizMicroInfo);
        JSONObject obj =JSONObject.parseObject(hqyResultMap.get("result").toString());
        if("0".equals(obj.getString("businessStatus"))){
            feiShunSettlementMapper.deleteById(feiShunSettlement.getId());//发起提现接口中有新增，所以此处删除，以免出现重复数据
            aja = AjaxResult.success("发起成功！");
            return aja;
        }else{
            aja = AjaxResult.error("发起失败:"+obj.getString("explain"));
            return aja;
        }
        //判断余额是否充足
//        BigDecimal balnce=bizWalletServiceImpl.queryBalanceFS();
//        if(balnce.compareTo(new BigDecimal(feiShunSettlement.getSettleAccount()))<0){
//            aja = AjaxResult.error("发起失败！余额不足");
//            return aja;
//        }
//        FeiShunSettlement settlement =new FeiShunSettlement();
//        settlement.setBankName(feiShunSettlement.getBankName());//开户行
//        settlement.setBankNum(feiShunSettlement.getBankNum());//申请人账号
//        settlement.setSettleAccount(feiShunSettlement.getSettleAccount());//结算金额
//        settlement.setUserName(feiShunSettlement.getUserName());//申请人名称
//        settlement.setUserNum(feiShunSettlement.getUserNum());//申请人身份证号
//        settlement.setUserTel(feiShunSettlement.getUserTel());//电话
//        settlement.setOrderNo(feiShunSettlement.getOrderNo());
//        settlement.setCreteDate(feiShunSettlement.getCreteDate());
//        //发起提现
//        aja=bizWalletServiceImpl.withdrawFS(settlement);
//        String codeDetail= aja.get("code").toString();
//        String messageDetail=(String) aja.get("msg");
//        if (codeDetail.equals("200")) {
//            feiShunSettlementMapper.deleteById(feiShunSettlement.getId());//发起提现接口中有新增，所以此处删除，以免出现重复数据
//            aja = AjaxResult.success("发起成功！");
//            return aja;
//        } else {
//            aja = AjaxResult.error("发起失败。"+messageDetail);
//            return aja;
//        }
    }


}
